package com.tripspins.app.bo;

import android.os.Parcel;
import android.os.Parcelable;

import com.tripspins.app.helper.FILE_TYPE;

import java.io.File;

public class Attachment implements Parcelable {
    /**
     *
     */
    private static final long serialVersionUID = 40578899749700454L;
    private boolean isSynced;
    private long id;
    private String path;
    private String thumbnailPath;
    private FILE_TYPE fileType;
    private Contact contact;
    private String publicFilePath;

    public FILE_TYPE getFileType() {
        return fileType;
    }

    public void setFileType(String fileType) {
        if(fileType.equals(FILE_TYPE.IMAGE.getValue()))
            this.fileType = FILE_TYPE.IMAGE;
        else if(fileType.equals(FILE_TYPE.VIDEO.getValue()))
            this.fileType = FILE_TYPE.VIDEO;
        else if(fileType.equals(FILE_TYPE.AUDIO.getValue()))
            this.fileType = FILE_TYPE.AUDIO;
        else if(fileType.equals(FILE_TYPE.CONTACT.getValue()))
            this.fileType = FILE_TYPE.CONTACT;


    }



    public Attachment()
    {
        id = -1;
        isSynced = false;


    }
    public boolean isSynced() {
        return isSynced;
    }

    public void setSynced(boolean synced) {
        isSynced = synced;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
        this.thumbnailPath = path.replace("ATTACHMENTS","THUMBNAILS");
    }

    public String getThumbnailPath()
    {
        if (path == null) return null;
        switch(fileType) {
            case AUDIO:
                return ("android.resource://ontheroad/drawable/img_pin_map");
            case VIDEO:
                return (thumbnailPath.replace("mp4","png"));
            case CONTACT:
                return (path);
            case IMAGE:
                return thumbnailPath;

        }
        return null;

    }




    @Override
    public String toString() {
        return "Attachment [id=" + id + ", path=" + path + "]";
    }

    public String getFileName() {
        if(path.compareTo("") == 0 ) return null;
        File file = new File(path);
        return file.getName();
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeLong(id);
        parcel.writeString(path);
        parcel.writeString(thumbnailPath);
        parcel.writeString(fileType.getValue());
        parcel.writeParcelable(contact,i);
    }

    private Attachment(Parcel parcel)
    {
        id = parcel.readLong();
        path = parcel.readString();
        thumbnailPath = parcel.readString();
        setFileType(parcel.readString());
        contact = parcel.readParcelable(Contact.class.getClassLoader());
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Attachment createFromParcel(Parcel parcel) {
            return new Attachment(parcel);
        }

        public Attachment[] newArray(int size) {
            return new Attachment[size];
        }
    };

    public void setPublicFilePath(String publicFilePath) {
        this.publicFilePath = publicFilePath;
    }

    public String getPublicFilePath() {
        return publicFilePath;
    }
}
